package net.hn.hnms.biz.dust.company;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.api.safety.dust.model.NowDataVo;
import net.hn.hnms.biz.api.safety.location.model.CompanyAlarmVo;
import net.hn.hnms.biz.api.mine.model.DustIndicatorVo;
import net.hn.hnms.biz.dust.domain.DwdFcfzNowSensormonitorEntity;
import net.hn.hnms.biz.dust.repository.FcfzNowRepository;
import net.hn.hnms.biz.dust.service.FcfzService;
import net.hn.hnms.biz.dust.vo.ChartVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.satoken.config.AutoSetContainOrgs;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 智呈物联 pan'sx
 * 2023/1/13 9:20
 * 1.0 version
 */
@RestController
@RequestMapping("company/safetyMonitor")
@Api(value = "粉尘防治控制器", tags = {"粉尘防治-公司端"})
@RequiredArgsConstructor
public class CompanyController {
    private final FcfzNowRepository fcfzNowRepository;
    private final FcfzService fcfzService;

    @GetMapping("nowData")
    @ApiOperation("实时监测")
    @AutoSetMineCode
    public R<NowDataVo> nowData() {
        BigDecimal dust = fcfzNowRepository.countByPointNameIsLike("%粉尘");
        BigDecimal dustAbnormal = fcfzNowRepository.countByPointNameLikeAndPointValueGreaterThan("%粉尘", new BigDecimal(40000));
        NowDataVo vo = new NowDataVo();
        vo.setDust(dustAbnormal.intValue() + dust.intValue());
        vo.setDustAbnormal(dustAbnormal.intValue());
        BigDecimal spray = fcfzNowRepository.countByPointNameLikeAndPointStatusCode("%阀门", "0");
        BigDecimal sprayAbnormal = fcfzNowRepository.countByPointNameLikeAndPointStatusCodeNot("%阀门", "0");
        vo.setSpray(spray.intValue() + sprayAbnormal.intValue());
        vo.setSprayAbnormal(sprayAbnormal.intValue());
        return R.ok(vo);
    }

    @GetMapping("alarm")
    @ApiOperation("实时告警")
    public R<List<CompanyAlarmVo.Info>> alarm() {
        List<DwdFcfzNowSensormonitorEntity> byStatusCodeNot = fcfzNowRepository.findByPointStatusCodeNot("0");
        List<CompanyAlarmVo.Info> infos = new ArrayList<>();
        CompanyAlarmVo vo = new CompanyAlarmVo();
        for (DwdFcfzNowSensormonitorEntity entity : byStatusCodeNot) {
            CompanyAlarmVo.Info info = vo.new Info();
            info.setMineName(Constants.mineCodeName.get(entity.getMineCode()));
            info.setAlarmType(entity.getPointStatusName());
            info.setBeginTime(entity.getDataTime());
            info.setCause(entity.getPointStatusName());
            infos.add(info);
        }
        return R.ok(infos);
    }

    @AutoSetContainOrgs
    @GetMapping("/score")
    @ApiOperation("粉尘安全评分")
    public R<List<DustIndicatorVo>> score(String [] containOrgs){
        return R.ok(fcfzService.score(containOrgs));
    }
    @GetMapping("/dustAlarmTrend")
    @ApiOperation("粉尘浓度告警趋势")
    public R<ChartVo> dustAlarmTrend(@ApiParam("周或月") @RequestParam(defaultValue = "周") String type, String mineCode) {
        return R.ok(fcfzService.alarmTrend(type,mineCode));
    }
    @GetMapping("/sprayAlarmTrend")
    @ApiOperation("喷淋告警趋势")
    public R<ChartVo> sprayAlarmTrend(@ApiParam("周或月") @RequestParam(defaultValue = "周") String type, String mineCode) {
        return R.ok(fcfzService.sprayAlarmTrend(type,mineCode));
    }


}
